<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>班级查询</title>

    <link rel="stylesheet" href="/static/js/layui-v2.5.7/layui/css/layui.css" media="all">
    <script type="text/javascript" src="/static/js/layui-v2.5.7/layui/layui.js"></script>
    <script type="text/javascript" src="/static/js/myJs.js"></script>


</head>
<body>
<div class="layui-container" align="center">
    <div class="layui-row">
        <div class="layui-col-md12">
            <h2 style="color:#4E5465;">
                班级列表
                <a href="/studentLayer" class="layui-btn layui-btn-radius layui-btn-xs">
                    菜单
                    <i class="layui-icon">&#xe614;</i>
                </a>

            </h2>
        </div>
    </div>
    <table id="table" lay-filter="table" lay-allowClose="true" class="layui-table">

    </table>
</div>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
        <button class="layui-btn layui-btn-sm layui-btn-radius layui-btn-primary" lay-event="add">添加班级</button>
        <button class="layui-btn layui-btn-sm layui-btn-radius layui-btn-primary" lay-event="shopping">购物车</button>
    </div>
</script>


<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script>

</script>

<!--<script type="text/javascript" src="http://owner.liuheng.com/static/js/jQuery-2.2.0.min.js"></script>-->
<script type="text/javascript">


    layui.use(['table', 'element'], function () {
        var table = layui.table;
        window.element = layui.element;
        var layer = layui.layer;

        table.render({
            elem: '#table',
            url: '/studentLayer/Classes/AjaxSelectClasses',
            height: '563',
            //  cellMinWidth:300,
            page: true,
            toolbar: '#toolbarDemo',
            parseData: function (res) {
                return {
                    "code": res.code,
                    "msg": res.msg,
                    "data": res.shop,
                    "count": res.count
                };
            }
            , cols: [[
                {field: 'id', title: 'ID', width: '120', sort: true, align: 'center'}
                , {field: 'name', title: '名字', width: '240', align: 'center'}
                , {field: 'id', title: '操作', align: 'center', toolbar: '#barDemo', width: '150'}
                , {type: 'checkbox', fixed: 'right'}
            ]]
        });


        table.on('tool(table)', function (obj) {
            var LayEvent = obj.event;
            var tr = obj.tr;
            var datatool = obj.data; //是一个对象，记住，是一个对象，必须要用对象的形式输出，不然会有问题的
            if (LayEvent == 'del') {
                layer.msg('是否删除', {
                    btn: ['是', '否']
                    , yes: function (index, layero) {
                        $.ajax({
                            dataType: 'json',
                            url: '/studentLayer/Classes/DeleteClasses',
                            type: 'GET',
                            data: {"id": datatool.id},
                            success: function (shop) {
                                if (shop.code == 0) {
                                    obj.del();//删除的是当前的结点，原本数据并没有删除
                                    table.reload("table");
                                    layer.close(index);
                                } else {
                                    layer.msg(shop.msg, function () {
                                        table.reload("table");
                                        layer.close(index);
                                    });
                                }
                            }
                        })
                    }
                    , btn2: function (index, layero) {
                        layer.close(index);
                    }
                })
            } else if (LayEvent == 'edit') {
                layer.msg('是否修改', {
                    btn: ['是', '否']
                    , btn1: function (index, layero) {
                        layer.open({
                            type: 2,
                            title: '修改',
                            area: ['100%', '100%'],
                            maxmin: true,
                            shade: '0.8',
                            shadClose: true,
                            content: '/studentLayer/Classes/UpdateClasses?id=' + datatool.id,
                        });
                    }
                    , btn2: function (index, layero) {
                        table.reload("#table");
                        layer.close(index);
                    }
                })
            }
        });


        table.on('toolbar', function (obj) {
            var checkStatus = table.checkStatus('table');
            var layEvent = obj.event;
            var datas = checkStatus.data;
              console.log(checkStatus.data);
             console.log(layEvent);
            switch (layEvent) {
                case'add':
                    layer.open({
                        type: 2,
                        title: '添加',
                        maxmin: true,
                        shade: '0.8',
                        shadeClose: 'true',
                        area: ['100%', '100%'],
                        content: '/studentLayer/Classes/InsertClasses',
                    })
                    break;
                case'shopping':
                    console.log(checkStatus.data)
                    $.ajax({
                        dataType:"json",
                        type:'POST',
                        url:'/studentLayer/Shopping/Shopping',
                        data:{"id":checkStatus.data},
                        // data:{"id":checkStatus.data},
                        success:function (data) {
                          //  console.log(data);
                            layer.alert(data.status);
                        }
                    })
                    break;
                case 'getCheckData':
                    layer.msg(JSON.stringify(checkStatus.data));//将js对象转换为字符串的形式
                    break;
                case'getCheckLength':
                    layer.msg('选中了:' + checkStatus.data.length + '条');
                    break;
                case'isAll':
                    layer.msg(checkStatus.isAll ? '全选' : '未全选');
                    break;


            }

        })
    });


</script>


</body>
</html>